package com.usian.skill.config;

import com.alibaba.fastjson.JSON;
import com.usian.skill.mapper.GoodsMapper;
import com.usian.skill.vo.GoodsVo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * ClassName: InitRedis
 * Author:   WGG
 * Date:    2022-12-2022/12/13-15:38
 * Version: 1.0
 * Description:
 */
@Component
public class InitRedis {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostConstruct //是spring框架的注解，，在方法上加该注解会在项目启动的时候执行该方法
    public void reload(){
        List<GoodsVo> goodsVoList = goodsMapper.findList();
        if (!CollectionUtils.isEmpty(goodsVoList)){
            for (GoodsVo goodsVo : goodsVoList) {
                stringRedisTemplate.opsForValue().set(goodsVo.getId().toString(), JSON.toJSONString(goodsVo));
            }
            stringRedisTemplate.opsForValue().set("goods", JSON.toJSONString(goodsVoList));
        }
    }

}
